package com.cheyunkeji.er.adapter.evaluate;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.activity.evaluate.EvaluateHomePageActivity;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.evaluate.EvaluateCarItemInfo;
import com.cheyunkeji.er.fragment.evaluate.CarFragment;
import com.cheyunkeji.er.utils.ImageLoadUtil;
import com.cheyunkeji.er.utils.TimeUtils;
import com.cheyunkeji.er.view.evaluate.SwipeMenuLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/** Created by Michael on 2017/3/23 17:50. */
public class CarFragLvAdapter extends MyBaseAdapter {

  private OnSwipeMenuClickedListener menuListener;

  public CarFragLvAdapter(ArrayList<EvaluateCarItemInfo> dataList, Context context) {
    super(dataList, context);

    setOnSwipeMenuClickedListener(
        (CarFragment) ((EvaluateHomePageActivity) context).getFragment(0));
    //
    // setOnSwipeMenuClickedListener((CarFragment)MyFragmentManager.getInstance().getFrag(CarFragment.class));

  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.er_evaluate_car_lv_item, null);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    EvaluateCarItemInfo bean = (EvaluateCarItemInfo) dataList.get(position);

    holder.tvCarBrand.setText(
        new StringBuffer().append(bean.getBrand_name()).append(" ").append(bean.getSeries_name()));
    ImageLoadUtil.loadImg(context, bean.getCover(), holder.ivCar);
    holder.tvCarPalateNumber.setText(
        String.format(
            context.getResources().getString(R.string.car_plate_number), bean.getPlate()));
    holder.tvEvaluateDate.setText(
        String.format(
            context.getResources().getString(R.string.evaluate_date2),
            TimeUtils.millis2StringWithoutHMS(
                Long.valueOf(String.valueOf(bean.getDateline()) + "000"))));

    holder.tv_evaluate_vin.setText(String.format("V I N : %1$s", bean.getVin()));
    holder.llDelete.setTag(position);
    holder.ivUpload.setTag(position);
    holder.rlLeftContent.setTag(position);

    if (((SwipeMenuLayout) convertView).isExpandCurrently())
      ((SwipeMenuLayout) convertView).quickClose();

    MyListener listener = new MyListener(convertView);

    holder.llDelete.setOnClickListener(listener);
    holder.ivUpload.setOnClickListener(listener);
    holder.rlLeftContent.setOnClickListener(listener);

    return convertView;
  }

  private class MyListener implements View.OnClickListener {

    private View convertView;

    public MyListener(View convertView) {
      this.convertView = convertView;
    }

    @Override
    public void onClick(View v) {

      switch (v.getId()) {
        case R.id.ll_delete:
          menuListener.onDeleteClicked((Integer) v.getTag());
          break;
        case R.id.iv_upload:
          menuListener.onUploadClicked((Integer) v.getTag());
          break;
        case R.id.rl_left_content:
          menuListener.onItemClicked((Integer) v.getTag());
          break;

        default:
          break;
      }
    }
  }

  static class ViewHolder {
    @BindView(R.id.iv_upload)
    ImageView ivUpload;

    @BindView(R.id.iv_car)
    ImageView ivCar;

    @BindView(R.id.iv_tag)
    ImageView ivTag;

    @BindView(R.id.tv_car_brand)
    TextView tvCarBrand;

    @BindView(R.id.tv_car_palate_number)
    TextView tvCarPalateNumber;

    @BindView(R.id.tv_evaluate_date)
    TextView tvEvaluateDate;
    //        @BindView(R.id.iv_edit)
    //        ImageView ivEdit;
    //        @BindView(R.id.ll_edit)
    //        LinearLayout llEdit;
    @BindView(R.id.iv_delete)
    ImageView ivDelete;

    @BindView(R.id.ll_delete)
    LinearLayout llDelete;

    @BindView(R.id.rl_left_content)
    RelativeLayout rlLeftContent;

    @BindView(R.id.tv_evaluate_vin)
    TextView tv_evaluate_vin;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }

  public interface OnSwipeMenuClickedListener {

    void onItemClicked(int position);

    void onUploadClicked(int position);

    void onDeleteClicked(int position);
  }

  public void setOnSwipeMenuClickedListener(OnSwipeMenuClickedListener listener) {
    this.menuListener = listener;
  }
}
